package com.zx.dsptuneup_TriMic;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.hjq.toast.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;
import com.zx.dsptuneup_TriMic.Data_Bean.EQBean;
import com.zx.dsptuneup_TriMic.ProtocolTools.Config;
import com.zx.dsptuneup_TriMic.Util.Constants;
import com.zx.dsptuneup_TriMic.ProtocolTools.ProtocolConversion;
import com.zx.dsptuneup_TriMic.Util.Netty.NettyClient;
import com.zx.dsptuneup_TriMic.Util.UserUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class App extends Application {
    private static final String TAG = "App";

    private static App mContext;
    private static EQBean eqData;    // 存储的EQ数据

    private NettyClient nettyClient;//socket操作连接对象
    private ProtocolConversion protocolConversion ;
    private static Activity topActivity = null;

    static public Activity getTopActivity() {
        return topActivity;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        eqData = new EQBean();
//        nettyClient = new NettyClient(Constants.HOST, Constants.TCP_PORT);
        protocolConversion = new ProtocolConversion();
        ToastUtils.init(this);
        Hawk.init(this).build();

        // 扫码库初始化
        ZXingLibrary.initDisplayOpinion(this);
        // 是否开发者
        if(Hawk.contains(UserUtil.DevelopMode) ){
            boolean flag = Hawk.get(UserUtil.DevelopMode);
            UserUtil.isDevelopMode = flag ? true : false;
        }
        initOkHttpUtil();
        // 初始化数据结构
        Config.configData();

        // 获取栈顶activity
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                topActivity = activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    /*
     * 初始化网络请求管理器
     * */
    private void initOkHttpUtil() {

        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        OkHttpClient client = new OkHttpClient.Builder()
                .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES))
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        request = request.newBuilder().addHeader("Connection", "keep-alive").build();
                        return chain.proceed(request);
                    }
                })
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
                .build();
        OkHttpUtils.initClient(client);
    }

    public void reinitSocket(){
        nettyClient = new NettyClient(Constants.HOST, Constants.TCP_PORT);
    }

    public ProtocolConversion getProtocolConversion() {
        return protocolConversion;
    }

    public NettyClient getNettyClient() {
        return nettyClient;
    }

    public static EQBean getEqData() {
        return eqData;
    }

    public static App getContext() {
        return mContext;
    }
}
